package com.example.translateappjava.Activity.HomeFragmentActivity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.example.translateappjava.Activity.MainActivity;
import com.example.translateappjava.R;
import com.example.translateappjava.Singleton.GlobalData;

public class BadResultFragment extends Fragment {

    private static final String ARG_IMAGE_URL = "image_url";
    private static final String ARG_SCIENTIFIC_BASIS = "scientific_basis";
    private static final String ARG_RECOGNITION_RESULT = "recognition_result";

    private String imageUrl;
    private String scientificBasis;
    private String recognitionResult;

    // newInstance 方法用于传递参数
    public static BadResultFragment newInstance(String imageUrl, String scientificBasis, String recognitionResult) {
        BadResultFragment fragment = new BadResultFragment();
        Bundle args = new Bundle();
        args.putString(ARG_IMAGE_URL, imageUrl);
        args.putString(ARG_SCIENTIFIC_BASIS, scientificBasis);
        args.putString(ARG_RECOGNITION_RESULT, recognitionResult);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_bad_result, container, false);

        // 从 Bundle 中获取传递的参数
        if (getArguments() != null) {
            imageUrl = getArguments().getString(ARG_IMAGE_URL);
            scientificBasis = getArguments().getString(ARG_SCIENTIFIC_BASIS);
            recognitionResult = getArguments().getString(ARG_RECOGNITION_RESULT);
        }

        TextView textViewScientificBasis = view.findViewById(R.id.badreasons);
        textViewScientificBasis.setText("   " + scientificBasis + "\n\n\n\n\n");

        TextView fruitname = view.findViewById(R.id.tv_top);
        fruitname.setText(recognitionResult);

        GlobalData globalData = GlobalData.getInstance();
        String value = globalData.getGlobalVariable();

        TextView warn = view.findViewById(R.id.question);
        warn.setText("这是"+value+"不建议食用的食物，你是否要继续食用？");

        if (imageUrl != null) {
            ImageView imageView = view.findViewById(R.id.imageView2);
            // 使用Glide加载网络图片
            Glide.with(this)
                .load(imageUrl)
                .into(imageView);
        }

        Button eatButton = view.findViewById(R.id.button2);
        eatButton.setOnClickListener(v -> showNoDialog());

        Button noteatButton = view.findViewById(R.id.button3);
        noteatButton.setOnClickListener(v -> showYesDialog());

        view.findViewById(R.id.btn_exit3).setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            requireActivity().finish();
        });

        return view;
    }

    private void showNoDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
        View dialogView = LayoutInflater.from(requireContext()).inflate(R.layout.eat, null);
        builder.setView(dialogView)
                .setPositiveButton("OK", (dialog, which) -> { /* 操作 */ })
                .setNegativeButton("Cancel", (dialog, which) -> dialog.dismiss())
                .create().show();
    }

    private void showYesDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
        View dialogView = LayoutInflater.from(requireContext()).inflate(R.layout.noeat, null);
        builder.setView(dialogView)
                .setPositiveButton("OK", (dialog, which) -> { /* 操作 */ })
                .setNegativeButton("Cancel", (dialog, which) -> dialog.dismiss())
                .create().show();
    }
}
